package com.weixing.mall.provider.controller.cms;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.weixing.mall.base.utils.EnumUtil;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.pay.channel.ali.AliPaySet;
import com.weixing.mall.pay.channel.wx.WxPaySet;
import com.weixing.mall.pay.enums.PayChannelEnum;
import com.weixing.mall.provider.api.model.vo.PaySetVo;
import com.weixing.mall.provider.model.domain.PayChannel;
import com.weixing.mall.provider.model.domain.PaySet;
import com.weixing.mall.provider.model.dto.cms.PaySetAliDto;
import com.weixing.mall.provider.model.dto.cms.PaySetWxDto;
import com.weixing.mall.provider.service.IPayChannelService;
import com.weixing.mall.provider.service.IPaySetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 支付设置 前端控制器
 * </p>
 *
 * @author Small
 * @since 2020-01-08
 */
@RestController
@RequestMapping("/payset")
@Api(value = "支付设置api", tags = "支付设置")
public class PaySetController extends BaseController {

    @Autowired
    private IPayChannelService payChannelService;
    @Autowired
    private IPaySetService paySetService;

    @ApiOperation(value = "支付渠道列表")
    @GetMapping("channel")
    public Result<List<PayChannel>> channelList() {
        QueryWrapper<PayChannel> queryWrapper = new QueryWrapper<PayChannel>().orderByAsc("id");
        List<PayChannel> list = payChannelService.list(queryWrapper);
        return ResultUtil.success(list);
    }

    @ApiOperation(value = "支付渠道保存、编辑")
    @PostMapping("channel")
    @NoRepeatSubmit
    public Result channelSave(PayChannel payChannel) {

        if (null == payChannel.getId()) {
            PayChannel channel = payChannelService.selectByCode(payChannel.getChannelCode());
            if (null != channel) {
                return ResultUtil.error("已存在同类型支付渠道");
            }
            payChannel.setChannelName(EnumUtil.getIEnum(PayChannelEnum.class, payChannel.getChannelCode()).getDesp());
            payChannelService.save(payChannel);
        } else {
            PayChannel byId = payChannelService.getById(payChannel.getId());
            if (!byId.getChannelCode().equals(payChannel.getChannelCode())) {
                return ResultUtil.error("禁止修改支付渠道");
            }
            payChannelService.updateById(payChannel);
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "支付渠道删除")
    @PostMapping("channel/del/{id}")
    public Result channelDel(@PathVariable Long id) {
        return payChannelService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }


    @ApiOperation(value = "根据支付渠道加载支付设置列表")
    @GetMapping("{channelId}")
    public Result<List> list(@PathVariable Long channelId) {
        PayChannel payChannel = payChannelService.getById(channelId);
        List<PaySetVo> list = paySetService.selectByChannelId(channelId);
        List result = Lists.newArrayList();
        if (null != list && !list.isEmpty()) {
            if (PayChannelEnum.WX_PAY.getValue().equals(payChannel.getChannelCode())){
                for (PaySetVo vo : list) {
                    WxPaySet wxPaySet = JSON.parseObject(vo.getParamSet(),WxPaySet.class);
                    PaySetWxDto dto = new PaySetWxDto();
                    dto.setId(vo.getId());
                    dto.setIsEnable(vo.getIsEnable());
                    dto.setChannelId(channelId);
                    dto.setEffectEndTime(vo.getEffectEndTime());
                    dto.setEffectStartTime(vo.getEffectStartTime());
                    dto.setName(vo.getName());
                    BeanUtils.copyProperties(wxPaySet,dto);
                    result.add(dto);
                }
            }else if (PayChannelEnum.ALI_PAY.getValue().equals(payChannel.getChannelCode())){
                for (PaySetVo vo : list) {
                    AliPaySet wxPaySet = JSON.parseObject(vo.getParamSet(),AliPaySet.class);
                    PaySetAliDto dto = new PaySetAliDto();
                    dto.setId(vo.getId());
                    dto.setIsEnable(vo.getIsEnable());
                    dto.setChannelId(channelId);
                    dto.setEffectEndTime(vo.getEffectEndTime());
                    dto.setEffectStartTime(vo.getEffectStartTime());
                    dto.setName(vo.getName());
                    BeanUtils.copyProperties(wxPaySet,dto);
                    result.add(dto);
                }
            }

        }
        return ResultUtil.success(result);
    }

    @ApiOperation(value = "微信支付设置保存、编辑")
    @PostMapping("wx")
    public Result saveWx(PaySetWxDto paySet) {
        PaySet pay = new PaySet();
        WxPaySet wxPaySet = new WxPaySet();
        BeanUtils.copyProperties(paySet, wxPaySet);
        BeanUtils.copyProperties(paySet, pay);
        pay.setParamSet(JSON.toJSONString(wxPaySet));
        pay.setIsEnable(paySet.getIsEnable().getValue());
        if (null == paySet.getId()) {
            paySetService.save(pay);
        } else {
            paySetService.updateById(pay);
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "支付宝支付设置保存、编辑")
    @PostMapping("ali")
    public Result saveAli(PaySetAliDto paySet) {
        PaySet pay = new PaySet();
        AliPaySet aliPaySet = new AliPaySet();
        BeanUtils.copyProperties(paySet, aliPaySet);
        BeanUtils.copyProperties(paySet, pay);
        pay.setParamSet(JSON.toJSONString(aliPaySet));
        pay.setIsEnable(paySet.getIsEnable().getValue());
        if (null == paySet.getId()) {
            paySetService.save(pay);
        } else {
            paySetService.updateById(pay);
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "支付设置删除")
    @PostMapping("del/{id}")
    public Result del(@PathVariable Long id) {
        return paySetService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }



}
